package com.school.base.action.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.school.base.model.Class;
import com.school.base.model.Teacher;
import com.school.base.service.ClassService;
import com.school.base.service.TeacherService;
import com.school.common.base.BaseAction;
import com.school.common.util.SpringUtils;
import com.school.common.util.SystemConfUtils;

public class ClassAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4848857157433227513L;
	private static Logger logger = Logger.getLogger(ClassAction.class);
	private static ClassService service = (ClassService) SpringUtils.getBean("base_classService");
	private Class cls;
	private List<Class> classList;

	public List<Teacher> getTeacherList() {
		List<Teacher> teacherList = new ArrayList<Teacher>();
		TeacherService teacherService = (TeacherService) SpringUtils.getBean("base_teacherService");
		teacherList = teacherService.getAll();
		return teacherList;
	}

	public Class getCls() {
		return cls;
	}

	public void setCls(Class cls) {
		this.cls = cls;
	}

	public List<Class> getClassList() {
		return classList;
	}

	public void setClassList(List<Class> classList) {
		this.classList = classList;
	}

	public String getGradeName(String grade) {
		String schooleType = SystemConfUtils.getProperty("system.schoole.type");
		String str = "school." + schooleType + ".grades";
		Map map = this.getI18nMap(str);
		try {
			return map.get(grade).toString();
		} catch (Exception e) {
			return "";
		}
	}

	public String list() {
		classList = service.getClassList(cls, pageBean);
		return SUCCESS;
	}

	public String edit() {
		if (cls != null && cls.getId() > 0) {
			cls = service.getClassById(cls.getId());
		}
		return SUCCESS;
	}

	public String editDo() {
		if ("POST".equals(this.getHttpServletRequest().getMethod())) {
			if (cls != null) {
				if (cls.getId() > 0) {
					try {
						service.updateClass(cls);
					} catch (Exception e) {
						this.addActionError(e.getMessage());
						return INPUT;
					}
				} else {
					try {
						service.insertClass(cls);
					} catch (Exception e) {
						this.addActionError(e.getMessage());
						return INPUT;
					}
				}
			} else {
				logger.info("Not save class:class object is null.");
			}
		} else {
			logger.info("Not save Class:the request method is not 'post'.");
		}
		return SUCCESS;
	}

	public String delete() {
		if (cls != null && cls.getId() > 0) {
			service.deleteClass(cls.getId());
			logger.info("Class is deleted");
		} else {
			logger.info("Not delete Class:class is null.");
		}
		return SUCCESS;
	}

}
